package com.wlyuan.index.infrastructure.converter;

import com.wlyuan.core.marker.NamedUtils;
import com.wlyuan.core.marker.ValueUtils;
import com.wlyuan.index.domain.index.WaybillIndex;
import com.wlyuan.index.domain.index.valueobj.Balances;
import com.wlyuan.index.domain.index.valueobj.WaybillRelation;
import com.wlyuan.index.domain.source.Waybill;
import com.wlyuan.index.domain.source.utils.SourceUtils;
import com.wlyuan.index.domain.source.valueobj.Balance;
import lombok.var;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.time.LocalDateTime;

public class WaybillIndexConverter {
    @Mapper(uses = {ValueUtils.class, NamedUtils.class, SourceUtils.class},
            imports = {LocalDateTime.class, BooleanUtils.class})
    public interface Converter {
        @Mapping(target = "balances", ignore = true)
        @Mapping(target = "customerId", source = "customer.customerId")
        @Mapping(target = "customerName", source = "customer.name")
        @Mapping(target = "customerType", source = "customer.type")
        @Mapping(target = "carrierId", source = "carrier.carrierId")
        @Mapping(target = "carrierName", source = "carrier.name")
        @Mapping(target = "carrierType", source = "carrier.type")
        @Mapping(target = "driverName", source = "driver.name")
        @Mapping(target = "driverId", source = "driver.driverId")
        @Mapping(target = "consigneeName", source = "consignee.name")
        @Mapping(target = "consigneeId", source = "consignee.consigneeId")
        @Mapping(target = ".", source = "arguments")
        @Mapping(target = ".", source = "statistics")
        @Mapping(target = "closeType", source = "close.type")
        @Mapping(target = "closeStatus", source = "close.status")
        @Mapping(target = "totalFee", source = "quote.totalFee")
        @Mapping(target = "prepayFee", source = "quote.prepayFee")
        @Mapping(target = "arrivePayFee", source = "quote.arrivePayFee")
        @Mapping(target = "collectionFee", source = "quote.collectionFee")
        @Mapping(target = "receiptFee", source = "quote.receiptFee")
        @Mapping(target = "combined", expression = "java(BooleanUtils.isTrue(source.getCombined()))")
        @Mapping(target = "indexAt", expression = "java(LocalDateTime.now())")
        WaybillIndex convert(Waybill source);


        //        @Mapping(target = "reconcileStatus", source = "balanceStatus")
        @Mapping(target = "callQuote", source = "canQuote")
        @Mapping(target = "callBill", source = "canBill")
        WaybillRelation convertRelation(com.wlyuan.index.domain.source.valueobj.WaybillRelation source);

        Balances convertBalance(Balance source);
    }

    private static final Converter CONVERTER = Mappers.getMapper(Converter.class);

    public static WaybillIndex convert(Waybill source) {
        if (null == source) {
            return null;
        }
        var index = CONVERTER.convert(source);

        if (CollectionUtils.isEmpty(index.getRelations())) {
            return index;
        }
        index.getRelations().forEach(relation -> {
            if (null != source.getBalance()) {
                var balances = CONVERTER.convertBalance(source.getBalance());
                relation.setBalances(Balances.split(balances, relation.getCharacter()));
            }
        });

        return index;
    }
}
